#!/bin/sh
# (c) Robert Shingledecker 2012
# added code for exquisite jlslegalize@gmail.com
# Load tce from iso file 
# At boot - called from tc-config with: iso=sdb1
# After boot in /opt/bootsync.sh with /usr/bin/fromISOfile sdb1
# After boot from command line with: sudo fromISOfile sdb1
# jlslegalize@gmail.com modifications for exquisiete boot splash and for second parameter indicating list of extesnions 
# to load
. /etc/init.d/tc-functions
. /etc/init.d/e-tc-functions #jls

alias awk="busybox awk"
alias depmod="busybox depmod"
alias mount="busybox mount"
alias umount="busybox umount"

checkroot
[ -z "$1" ] && exit 1

#jls start
WRITE="/usr/local/bin/exquisite-write"
PROGRESS=0
EXQUISITE="$(pidof exquisite)"
#jls end


read USER < /etc/sysconfig/tcuser || exit 1
[ -d /mnt/cdrom ] || mkdir /mnt/cdrom
TARGET="$1"
TARGET="${TARGET#/mnt/}"
TARGET="${TARGET#/dev/}"
DEVICE="${TARGET%%/*}"
if [ "${TARGET##*.}" == "iso" ]
then
	TARGET="${TARGET#*/}"
else
	TARGET=""
fi

find_mountpoint "$DEVICE"
if [ -z "$MOUNTPOINT" ]; then
	echo "Invalid device specified: $DEVICE"
	exit 1
fi
if ! mounted "$MOUNTPOINT"; then
	mount "$MOUNTPOINT"
	if [ "$?" != 0 ]; then
		echo "Unable to mount drive!"
		[ "$EXQUISITE" ] && $WRITE "TEXT Unable to mount drive!" && $WRITE "FAILURE FAIL" #jls
		exit 1
	fi
fi
if [ -z "$TARGET" ]; then
	find "$MOUNTPOINT" -name "*.iso" > /tmp/isos.lst
	FOUND="$(awk '{print NR}' /tmp/isos.lst 2>/dev/null)" || FOUND=0
	if [ "$FOUND" == 0 ]; then
		echo "No iso file found on /mnt/$DEVICE"
		[ "$EXQUISITE" ] && $WRITE "TEXT No iso file found on /mnt/$DEVICE" && $WRITE "FAILURE FAIL" #jls
		exit 1
	fi
	if [ "$FOUND" == 1 ]; then
		TARGET=$(cat /tmp/isos.lst)
	else
		echo "Multiple isos found, please use full path."
		[ "$EXQUISITE" ] && $WRITE "TEXT Multiple isos found, please use full path." && $WRITE "FAILURE FAIL" #jls
		cat /tmp/isos.lst
		exit 1
	fi
else
	TARGET="$MOUNTPOINT"/"$TARGET"
fi
echo "$TARGET"
mount -t iso9660 -o ro "$TARGET" /mnt/cdrom
if [ "$?" != 0 ]; then
	echo "Failed to mount specified $TARGET"
	[ "$EXQUISITE" ] && $WRITE "TEXT Failed to mount specified $TARGET" && $WRITE "FAILURE FAIL" #jls
	exit 1
fi
if [ ! -d /mnt/cdrom/tce ]; then
	echo "no tce directory found in $TARGET"
	[ "$EXQUISITE" ] && $WRITE "TEXT no tce directory found in $TARGET" && $WRITE "FAILURE FAIL" #jls
	umount /mnt/cdrom
	exit 1
fi
LOADFROM=/mnt/cdrom/tce/optional
#jls start
if [ -z "$2" ] ; then #jls end
	TCELIST="/mnt/cdrom/tce/onboot.lst"
#jls start
else
	TCELIST="/mnt/cdrom/tce/$2"
fi #jls end
echo "Loading $TCELIST from embedded tce directory."
[ "$EXQUISITE" ] && $WRITE "MSG Loading $TCELIST from embedded tce directory." #jls
if [ -s "$TCELIST" ]; then
	percent_calc "$TCELIST" #jls
	while read FILE; do
		FILE="$LOADFROM/$FILE"
		BASENAME=`basename "$FILE"`
		APPNAME="${BASENAME%.tcz}"
		if [ ! -f "$TCEINSTALLED"/"$APPNAME" ]; then
			FLAGS=" -i -b "
			su "$USER" -c 'tce-load '"$FLAGS"' '"$FILE"
			if [ -s /tmp/aberr ]; then
				echo "occured while loading: " "$FILE" >> /tmp/aberr
				[ "$EXQUISITE" ] && $WRITE "TEXT occured while loading: $FILE" && $WRITE "FAILURE FAIL" #jls
				umount /mnt/cdrom
				exit 1
			fi
			[ "$EXQUISITE" ] && PROGRESS=$(($PROGRESS + $PERCENT )) && $WRITE "PROGRESS $PROGRESS" && $WRITE "TEXT $FILE loaded" && $WRITE "SUCCESS OK" #jls
		fi
	done < "$TCELIST"
	echo "$LOADFROM" > /etc/sysconfig/cde
	[ "$EXQUISITE" ] && $WRITE "TEXT Loading $TCELIST from embedded tce directory." && $WRITE "SUCCESS OK" && $WRITE "MSG  " #jls
else
	echo "No onboot.lst from in tce directory."
	[ "$EXQUISITE" ] && $WRITE "TEXT No onboot.lst from in tce directory." #jls
	umount /mnt/cdrom
	exit 1
fi                                                                                               

# Wrap up by performing updates as may be needed.
if [ -e /etc/sysconfig/newmodules ]; then
	depmod -a 2>/dev/null
	/sbin/udevadm trigger
fi
/sbin/ldconfig 2>/dev/null
if [ -s /tmp/setup.lst ]; then
	for F in `cat /tmp/setup.lst`; do "$F"; done
	rm -f /tmp/setup.lst
fi
setupHome
